home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
Tickle-4.0 (tcl)
/
src
/
tclMacCWD.c
< prev
next >
Wrap
Text File
|
1993-11-18
|
9KB
|
410 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
*/
/*
search -s GetIndString {ci}≈.h
*/
#pragma segment TCL
#include <resources.h>
#include <memory.h>
#include <files.h>
#include <GestaltEqu.h>
#include <string.h>
#include <packages.h>
#include <folders.h>
#include <ToolUtils.h>
#include <errors.h>
#include <stdarg.h>
#include <Folders.h>
#include <Sound.h>
#include <Traps.h>
#include <Errors.h>
#include "tcl.h"
#include "tclMac.h"
#include "XTCL.h"
#include "stat.h"
#include "version.h"
char *tcl_check_path_termination( char *path );
/*
** NOTE
**
** The entire Mac TCL current working directory (CWD) library
** lives on two assumtions:
**
** 1) You have a hierarchical file system.
**
** 2) You call Tcl_InitializeCurrentWorkingDirectory()
** at the point you want the Macintosh Default Directory
** to become the tcl current working directory.
**
*/
/*
** The following code is used to maintain the
** "Current Working Directory" state for tcl.
*/
static short _current_working_vrefnum = 0;
static long _current_working_dirid = fsRtDirID;
static short _current_working_wdrefnum = 0;
static short _current_wdrefnum_count = 0;
#define MAX_PUSH_DIRS 4
static long _tcl_push_dirs[MAX_PUSH_DIRS];
static short _tcl_push_vols[MAX_PUSH_DIRS];
static int _tcl_push_index = 0;
/*
** This function is used to set the tcl current working directory
** to the volume "vRefNum" and directory "dirID". In doing so, the
** function also creates a working directory for the cwd.
**
** The function then calls SetVol() to set the Macintosh Default
** Directory to the same location.
**
** The function returns the result of PBOpenWD() && SetVol().
**
** If PBOpenWD() or SetVol() returns an error, the cwd is not set.
*/
int
TclMac_CWDChgDir(vRefNum, dirID)
int vRefNum;
long dirID;
{
int myerr;
short oldwd;
WDPBRec wpb;
Str32 volname;
char pathbuf[2048],
*ptr;
if ( vRefNum == _current_working_vrefnum && dirID == _current_working_dirid )
{
myerr = SetVol(NULL, _current_working_wdrefnum);
if ( myerr != noErr )
mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: SetVol(%d) = %d\n",
wpb.ioVRefNum, myerr);
return myerr;
}
wpb.ioCompletion = 0;
wpb.ioNamePtr = NULL;
wpb.ioVRefNum = vRefNum;
wpb.ioWDDirID = dirID;
wpb.ioWDProcID = 'MTcl';
myerr = PBOpenWD(&wpb, FALSE);
if (myerr != noErr)
{
mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: PBOpenWD(%d, %d) = %d\n",
vRefNum, dirID, myerr);
}
else
{
myerr = SetVol(NULL, wpb.ioVRefNum);
if ( myerr != noErr )
{
mac_fprintf(stderr, "ERROR: TclMac_CWDChgDir: SetVol(%d) = %d\n",
wpb.ioVRefNum, myerr);
wpb.ioCompletion = 0;
wpb.ioNamePtr = NULL;
wpb.ioWDDirID = 0;
wpb.ioWDProcID = 'MTcl';
PBCloseWD(&wpb, FALSE);
}
else
{
oldwd = _current_working_wdrefnum;
_current_working_wdrefnum = wpb.ioVRefNum;
if ( oldwd != 0 && oldwd != wpb.ioVRefNum && _current_wdrefnum_count == 0 )
{
wpb.ioCompletion = 0;
wpb.ioNamePtr = NULL;
wpb.ioVRefNum = oldwd;
wpb.ioWDDirID = 0;
wpb.ioWDProcID = 'MTcl';
PBCloseWD(&wpb, FALSE);
}
_current_working_vrefnum = vRefNum;
_current_working_dirid = dirID;
SFSaveDisk = _current_working_vrefnum * -1;
CurDirStore = _current_working_dirid;
dirpathname( pathbuf, _current_working_vrefnum,
_current_working_dirid );
tcl_check_path_termination(pathbuf);
TclSetEnv(kDefaultDirTag, pathbuf);
}
}
return myerr;
}
/*
** This function is used to set the Macintosh Default Directory
** to the tcl current working directory using the SetVol() call,
** and the working directory created for the cwd.
**
** The function returns the result of SetVol().
*/
int
TclMac_CWDSetVol()
{
SetVol(NULL, _current_working_wdrefnum);
return noErr;
}
/*
** The following routines provide the following commonly
** used construct:
**
** {
** TclMac_CWDPushVol(); -- Save the current default directory
** TclMac_CWDSetVol(); -- Set the default directory to tcl's cwd
**
** ... code using the tcl cwd ...
**
** TclMac_CWDPopVol(); -- Restore the current default directory
** }
**
** There MUST be exactly one TclMac_CWDPopVol() for each TclMac_CWDPushVol()!
**
*/
/*
** This function pushes the current Macintosh Default Directory
** (as returned by PBHGetVol()) onto a stack.
**
** The function returns the result of PBHGetVol().
**
** If PBHGetVol() returns an error, the push is not performed.
** This function has *no* effect on the tcl cwd.
*/
int
TclMac_CWDPushVol()
{
int myerr;
Str32 volname;
WDPBRec pb;
if (_tcl_push_index >= MAX_PUSH_DIRS)
{
mac_fprintf(stderr, "TclMac_CWDPushVol: WARNING - OVERFLOW stack.\n");
return queueFull;
}
pb.ioCompletion = 0;
pb.ioNamePtr = volname; volname[0] = '\0';
myerr = PBHGetVol(&pb, FALSE);
if (myerr == noErr)
{
_tcl_push_vols[_tcl_push_index] = pb.ioVRefNum;
_tcl_push_dirs[_tcl_push_index] = pb.ioWDDirID;
++_tcl_push_index;
}
return myerr;
}
/*
** This function pops the current Macintosh Default Directory
** from a stack and calls PBHSetVol() to set the default directory.
**
** The function returns the result of PBHSetVol().
**
** This function has *no* effect on the tcl cwd.
*/
int
TclMac_CWDPopVol()
{
int myerr;
WDPBRec pb;
if (_tcl_push_index < 1)
{
mac_fprintf(stderr, "TclMac_CWDPopVol: WARNING - UNDERFLOW stack.\n");
return unitEmptyErr;
}
--_tcl_push_index;
pb.ioCompletion = 0;
pb.ioNamePtr = NULL;
pb.ioVRefNum = _tcl_push_vols[_tcl_push_index];
pb.ioWDDirID = _tcl_push_dirs[_tcl_push_index];
myerr = PBHSetVol(&pb, FALSE);
return myerr;
}
/*
** This function returns the tcl current working directory's
** volume reference number. This is never a wdrefnum.
*/
int
TclMac_CWDVRefNum()
{
return _current_working_vrefnum;
}
/*
** This function returns the tcl current working directory's
** directory ID.
*/
int
TclMac_CWDDirID()
{
return _current_working_dirid;
}
/*
** This function returns the tcl current working directory's
** full pathname.
*/
char *
TclMac_CWDPathName(path)
char *path;
{
dirpathname(path, _current_working_vrefnum, _current_working_dirid);
tcl_check_path_termination(path);
return path;
}
/*
** The following routines provide the following commonly
** used construct:
**
** {
** TclMac_CWDCreateWD(&myWDRefNum); -- Create current default directory WD
**
** ... code using myWDRefNum ...
**
** TclMac_CWDDisposeWD(wdRefNum); -- Close the created WD
** }
**
** You *really* want to dispose of these WD's quickly and use
** very sparingly (mostly to support old code you don't want
** to recode to use dirid's). WD's are a limited system resource.
**
*/
/*
** This function creates a Macintosh Working Directory that
** references the tcl current working directory.
**
** The result is the result of the PBOpenWD() call.
**
** If the result is noErr, the working directory reference
** number is copied into wdRefNum
**
** This function has *no* effect on the tcl cwd.
*/
int
TclMac_CWDCreateWD(wdRefNum)
short *wdRefNum;
{
++_current_wdrefnum_count;
*wdRefNum = _current_working_wdrefnum;
return noErr;
}
/*
** This function disposes a Macintosh Working Directory that
** was created by a call to TclMac_CWDCreateWD().
**
** The result is the result of the PBCloseWD() call.
**
** This function has *no* effect on the tcl cwd.
*/
int
TclMac_CWDDisposeWD(wdRefNum)
short wdRefNum;
{
WDPBRec wpb;
if (_current_wdrefnum_count < 1)
{
mac_fprintf(stderr, "ERROR: TclMac_CWDDisposeWD: count is ZERO.\n");
}
if (--_current_wdrefnum_count == 0 && wdRefNum != _current_working_wdrefnum)
{
wpb.ioCompletion = 0;
wpb.ioNamePtr = NULL;
wpb.ioVRefNum = wdRefNum;
wpb.ioWDDirID = 0;
wpb.ioWDProcID = 'MTcl';
PBCloseWD(&wpb, FALSE);
}
return noErr;
}
/*
** This function initializes the tcl current working directory.
**
** The function uses PBHGetVol() to get the Macintosh Default
** Directory, and sets the tcl cwd to that directory.
**
** The result is always TCL_OK
**
** The application must call this function once when it wishes
** to establish the tcl cwd. This is usally performed immediately
** after the application is launched to cause the tcl cwd to be
** initialized to the directory containing the application.
*/
int
TclMac_CWDInitialize()
{
int myerr;
Str32 volname;
WDPBRec wpb;
_current_wdrefnum_count = 0;
wpb.ioCompletion = 0;
wpb.ioNamePtr = volname; volname[0] = '\0';
myerr = PBHGetVol( &wpb, FALSE );
if (myerr != noErr)
{
_current_working_dirid = fsRtDirID;
_current_working_vrefnum = 0;
_current_working_wdrefnum = 0;
}
else
{
_current_working_dirid = wpb.ioWDDirID;
_current_working_vrefnum = wpb.ioWDVRefNum;
_current_working_wdrefnum = wpb.ioVRefNum;
//GetVol(NULL, &_current_working_wdrefnum);
}
return TCL_OK;
}